import timeit
import numba


def cal_sum(n:int) -> int:
    sum: float = 0.0
    for i in range(n):
        sum = i**2 + sum
    return sum

@numba.jit
def cal_sum_jit(n:int) -> int:
    sum: float = 0.0
    for i in range(n):
        sum = i**2 + sum
    return sum

if __name__ == '__main__':
    ret1 = timeit.timeit("cal_sum(20000000)", setup="from __main__ import cal_sum", number=1)
    ret2 = timeit.timeit("cal_sum_jit(20000000)", setup="from __main__ import cal_sum_jit", number=1)
    ret3 = timeit.timeit("cal_sum_jit(30000000)", setup="from __main__ import cal_sum_jit", number=1)
    print(f"常规计算时间: {ret1:.10f}")
    print(f"JIT计算时间: {ret2:.10f}")
    print(f"JIT 第二次计算时间: {ret3:.10f}")

"""
常规计算时间: 4.9387498330
JIT计算时间: 0.6860353110
JIT 第二次计算时间: 0.0196715000

结论: 使用JIT加速后,计算速度提升
"""